﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LibGUI.Schedule;
using LibRTC;
using LibRTC.Core;
using LibRTC.Core.MessageMap.BuddyEvent;
using RTCCore;

namespace LibGUI.MainForm.UserList {

    class UserTreeView : BaseUserTreeView, IBuddyStatusChange
    {
        public UserTreeView()
        {
            // do some init here
            this.DoubleClick += OnDoubleClick;
            this.DrawMode = TreeViewDrawMode.OwnerDrawText;
            this.DrawNode += new DrawTreeNodeEventHandler(OnOwnerDrawNode);
            
            AddGroup("在线好友");
            AddGroup("离线好友");
            AddGroup("陌生人");
        }

        protected void OnDoubleClick(Object sender, EventArgs args)
        {
            MouseEventArgs eventArgs = (MouseEventArgs)args;

            TreeNode node = GetNodeAt(eventArgs.Location);

            // double click a Node
            if(null != node && (null != (node as BaseUserNode)))
            {
                BaseUserNode userNode = node as BaseUserNode;
                MessageDispatcher.GetInstance().CreateAddChatForm(_rtcSystem.LocalUserName,
                                                                  _rtcSystem.LocalUserURI,
                                                                  userNode.UserName, userNode.UserURI);
            }
        }

        protected void OnOwnerDrawNode(object sender, DrawTreeNodeEventArgs e)
        {
            BaseUserNode userNode = e.Node as BaseUserNode;

            if(null == userNode) {
                DrawGroup(e.Graphics,e.Bounds,e.Node);
            }
            else {
                DrawUserNode(e.Graphics, e.Bounds, userNode);
            }
        }

        private void DrawGroup(Graphics graphics,Rectangle rect, TreeNode node) {
            Brush backgroundBrush = new SolidBrush(Color.Blue);
            graphics.DrawRectangle(new Pen(Color.Red, 1), rect);
            graphics.DrawString(node.Name, new Font("", 8, FontStyle.Regular), backgroundBrush, rect);             
        }

        private void DrawUserNode(Graphics graphics,Rectangle rect, BaseUserNode userNode) {
            Brush backgroundBrush = new SolidBrush(Color.Gray);

            if (RTC_PRESENCE_STATUS.RTCXS_PRESENCE_ONLINE == userNode.UserStatus)
            {
                backgroundBrush = new SolidBrush(Color.Green);
            }

            graphics.DrawRectangle(new Pen(Color.Red, 1), rect);
            graphics.DrawString(userNode.UserName, new Font("", 8, FontStyle.Regular), backgroundBrush, rect);            
        }

        public void OnBuddyStatusChange(string userURI, RTC_PRESENCE_STATUS status) {

            BaseUserNode node = RemoveUserNode(userURI);

            if(RTC_PRESENCE_STATUS.RTCXS_PRESENCE_ONLINE == status) {
                AddUserNode("在线好友",node);
            }
            else {
                AddUserNode("离线好友", node);
            }

        }
    }
}